<template>
  <div class="loading-screen">
    <div class="loading-container">
      <!-- Logo区域 -->
      <div class="logo-section">
        <img src="@/assets/logo.png" alt="Logo" class="logo" />
        <h1 class="app-title">氮化镓紫外局放监测系统</h1>
      </div>

      <!-- 动画区域 -->
      <div class="animation-section">
        <div class="loading-spinner">
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
        </div>

        <!-- 进度条 -->
        <div class="progress-container">
          <div class="progress-bar">
            <div class="progress-fill" :style="{ width: progress + '%' }"></div>
          </div>
          <div class="progress-text">{{ loadingText }}</div>
        </div>
      </div>

      <!-- 版本信息 -->
      <div class="version-info">
        <p>版本 v1.0.0</p>
        <p>正在初始化系统...</p>
      </div>
    </div>

    <!-- 背景粒子效果 -->
    <div class="particles">
      <div
        v-for="i in 50"
        :key="i"
        class="particle"
        :style="getParticleStyle(i)"
      ></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue";
import { storeToRefs } from "pinia";
import { adminStore } from "@/store/admin";

const admin_store = adminStore();

// 定义emit事件
const emit = defineEmits<{
  "loading-complete": [];
}>();

const progress = ref(0);
const loadingText = ref("正在启动系统...");

const loadingSteps = [
  { text: "Starting System...", duration: 1200 },
  { text: "Connecting to Backend Service...", duration: 1500 },
  { text: "Fetching Port Configuration...", duration: 1200 },
  { text: "Initializing Gateway Connection...", duration: 1000 },
  { text: "System Ready...", duration: 600 },
];

let progressTimer: number | null = null;
let stepIndex = 0;

const startLoading = () => {
  let currentProgress = 0;
  const totalDuration = loadingSteps.reduce(
    (sum, step) => sum + step.duration,
    0
  );
  let elapsedTime = 0;

  const updateProgress = () => {
    if (stepIndex < loadingSteps.length) {
      const currentStep = loadingSteps[stepIndex];
      loadingText.value = currentStep.text;

      elapsedTime += 50;
      currentProgress = Math.min((elapsedTime / totalDuration) * 100, 100);
      progress.value = currentProgress;

      if (
        elapsedTime >=
        loadingSteps
          .slice(0, stepIndex + 1)
          .reduce((sum, step) => sum + step.duration, 0)
      ) {
        stepIndex++;
      }

      if (currentProgress >= 100) {
        console.log("Loading complete.");
        // 发出加载完成事件
        console.log("发出loading-complete事件");
        emit("loading-complete");
      } else {
        progressTimer = setTimeout(updateProgress, 50);
      }
    }
  };

  updateProgress();
};

const getParticleStyle = (index: number) => {
  const size = Math.random() * 4 + 2;
  const left = Math.random() * 100;
  const animationDelay = Math.random() * 3;
  const animationDuration = Math.random() * 3 + 2;

  return {
    width: `${size}px`,
    height: `${size}px`,
    left: `${left}%`,
    animationDelay: `${animationDelay}s`,
    animationDuration: `${animationDuration}s`,
  };
};

onMounted(() => {
  startLoading();
});

onUnmounted(() => {
  if (progressTimer) {
    clearTimeout(progressTimer);
  }
});
</script>

<style scoped>
.loading-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #0f1419 0%, #1a2332 50%, #0f1419 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  overflow: hidden;
}

.loading-container {
  text-align: center;
  color: #fff;
  z-index: 2;
  position: relative;
}

.logo-section {
  margin-bottom: 40px;
}

.logo {
  width: 160px;
  height: 160px;
  object-fit: contain;
  margin-bottom: 20px;
  filter: drop-shadow(0 0 20px rgba(29, 233, 182, 0.5));
  animation: logoGlow 2s ease-in-out infinite alternate;
}

@keyframes logoGlow {
  from {
    filter: drop-shadow(0 0 20px rgba(29, 233, 182, 0.5));
  }
  to {
    filter: drop-shadow(0 0 30px rgba(29, 233, 182, 0.8));
  }
}

.app-title {
  font-size: 56px; /* 直接增大基础字体大小 */
  font-weight: bold;
  margin: 0;
  background: linear-gradient(135deg, #1de9b6 0%, #1dc8e9 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.animation-section {
  margin: 40px 0;
}

.loading-spinner {
  position: relative;
  width: 140px;
  height: 140px;
  margin: 0 auto 30px;
}

.spinner-ring {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 3px solid transparent;
  border-radius: 50%;
  animation: spin 2s linear infinite;
}

.spinner-ring:nth-child(1) {
  border-top-color: #1de9b6;
  animation-delay: 0s;
}

.spinner-ring:nth-child(2) {
  border-right-color: #1dc8e9;
  animation-delay: 0.3s;
  width: 70%;
  height: 70%;
  top: 15%;
  left: 15%;
}

.spinner-ring:nth-child(3) {
  border-bottom-color: #5470c6;
  animation-delay: 0.6s;
  width: 40%;
  height: 40%;
  top: 30%;
  left: 30%;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.progress-container {
  width: 300px;
  margin: 0 auto;
}

.progress-bar {
  width: 100%;
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 15px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #1de9b6 0%, #1dc8e9 100%);
  border-radius: 2px;
  transition: width 0.3s ease;
  box-shadow: 0 0 10px rgba(29, 233, 182, 0.5);
}

.progress-text {
  font-size: 22px; /* 直接增大进度文字 */
  color: #b0c4de;
  margin-top: 10px;
}

.version-info {
  margin-top: 40px;
  font-size: 20px; /* 直接增大版本信息字体 */
  color: #7a8a9e;
}

.version-info p {
  margin: 5px 0;
}

.particles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.particle {
  position: absolute;
  background: rgba(29, 233, 182, 0.6);
  border-radius: 50%;
  animation: float linear infinite;
}

@keyframes float {
  0% {
    transform: translateY(100vh) rotate(0deg);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateY(-10vh) rotate(360deg);
    opacity: 0;
  }
}

/* 响应式设计 - 针对小窗口 */
@media (max-width: 1200px) {
  .app-title {
    font-size: 68px; /* 增大字体，适应小窗口 */
  }

  .logo {
    width: 180px; /* 稍微增大logo */
    height: 180px;
  }

  .progress-container {
    width: 550px; /* 增大进度条容器 */
  }

  .progress-text {
    font-size: 25px; /* 增大进度文字 */
  }

  .loading-spinner {
    width: 160px; /* 稍微增大加载动画 */
    height: 160px;
  }

  .version-info {
    font-size: 24px; /* 增大版本信息字体 */
  }
}

/* 针对更小的窗口 */
@media (max-width: 800px) {
  .app-title {
    font-size: 36px;
  }

  .logo {
    width: 120px;
    height: 120px;
  }

  .progress-container {
    width: 280px;
  }

  .progress-text {
    font-size: 18px;
  }

  .loading-spinner {
    width: 80px;
    height: 80px;
  }

  .version-info {
    font-size: 16px;
  }
}
</style>